package org.bpms.controller.admin;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bpms.model.admin.Book;
import org.bpms.model.admin.Booktemp;
import org.bpms.model.admin.Suser;
import org.bpms.plugins.bjui.BjuiRender;
import org.bpms.service.admin.BookService;
import org.bpms.service.admin.BooktempService;

import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
/**
 * 临时档案控制器
 * @author hupei
 *
 */
public class BooktempController extends Controller{
	private BooktempService booktempService = Enhancer.enhance(BooktempService.class);
	private BookService bookService = Enhancer.enhance(BookService.class);
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 查询统一tempid的临时数据 A节点
	 */
	public void index(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		int random = getParaToInt("random" , 0);
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Booktemp> lb = booktempService.findFromrandom(random , "A", user.getStr("id"));
		booktempService.findTempToPut(random , lb, pageNumber, pageSize, 
				orderField, orderDirection, pagemap);
		setAttr("page",pagemap);
		setAttr("tabid", tabid);
		setSessionAttr("random", random);
		setAttr("random", random);
		render("/admin/book/shoujitemp.html");
	}
	
	/**
	 * 查询统一tempid的临时数据 c节点
	 */
	public void indexc(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		int random = getParaToInt("random" , 0);
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Booktemp> lb = booktempService.findFromrandom(random , "C", user.getStr("id"));
		booktempService.findTempToPut(random , lb, pageNumber, pageSize, 
				orderField, orderDirection, pagemap);
		setAttr("page",pagemap);
		setAttr("tabid", tabid);
		setSessionAttr("random", random);
		setAttr("random", random);
		render("/admin/book/shoujitempc.html");
	}	
	
	/**
	 * 查询统一tempid的临时数据 d节点
	 */
	public void indexd(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		int random = getParaToInt("random" , 0);
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Booktemp> lb = booktempService.findFromrandom(random , "D", user.getStr("id"));
		booktempService.findTempToPut(random , lb, pageNumber, pageSize, 
				orderField, orderDirection, pagemap);
		setAttr("page",pagemap);
		setAttr("tabid", tabid);
		setSessionAttr("random", random);
		setAttr("random", random);
		render("/admin/book/shoujitempd.html");
	}	
	
	/**
	 * 选择性A覆盖
	 */
	public void past(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		int random = getParaToInt("random" , 0);
		if (random == 0){
			random = getSessionAttr("random");
		}
		List<Booktemp> lb = booktempService.findAllTemp(random);
		String delids = getPara("delids" , "");
		if (!delids.equals("")){
			String ids[] = delids.split(",");
			for (Booktemp temp : lb){
				for (String id:ids){
					if (id.equals(temp.get("id"))){
						String tempid = temp.get("id");
						Book book = Book.me.findFirst("select * from book where bookid=?" , temp.get("bookid"));
						if (book.get("state").equals("A")){
							temp.set("id", book.get("id"));
							temp.set("creater", book.get("creater"));
							temp.set("ctime", Timestamp.valueOf(df.format(new Date())));
							temp.set("typeid", book.get("typeid"));
							temp.set("state", book.get("state"));
							temp.set("color", book.get("color"));
							book.setAttrs(temp.getAll());
							book.update();		
							Booktemp.me.deleteById(tempid);
						}
					}
				}
			}
		}
		//booktempService.remove(random);
		render(BjuiRender.success("覆盖成功！" , false).tabid(tabid));
	}
	
	/**
	 * 选择性覆盖 C节点
	 */
	public void pastc(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		int random = getParaToInt("random" , 0);
		if (random == 0){
			random = getSessionAttr("random");
		}
		List<Booktemp> lb = booktempService.findAllTemp(random);
		String delids = getPara("delids" , "");
		if (!delids.equals("")){
			String ids[] = delids.split(",");
			for (Booktemp temp : lb){
				for (String id:ids){
					if (id.equals(temp.get("id"))){
						String tempid = temp.get("id");
						Book book = Book.me.findFirst("select * from book where bookid=?" , temp.get("bookid"));
						if (book.get("state").equals("C")){
							temp.set("id", book.get("id"));
							temp.set("creater", book.get("creater"));
							temp.set("ctime", Timestamp.valueOf(df.format(new Date())));
							temp.set("typeid", book.get("typeid"));
							temp.set("color", book.get("color"));
							temp.set("state", "D");
							book.setAttrs(temp.getAll());
							book.update();		
							Booktemp.me.deleteById(tempid);
						}
					}
				}
			}
		}
		//booktempService.remove(random);
		render(BjuiRender.success("覆盖成功！" , false).tabid(tabid));
	}
	
	/**
	 * 选择性覆盖 D节点
	 */
	public void pastd(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		int random = getParaToInt("random" , 0);
		if (random == 0){
			random = getSessionAttr("random");
		}
		List<Booktemp> lb = booktempService.findAllTemp(random);
		String delids = getPara("delids" , "");
		if (!delids.equals("")){
			String ids[] = delids.split(",");
			for (Booktemp temp : lb){
				for (String id:ids){
					if (id.equals(temp.get("id"))){
						String tempid = temp.get("id");
						Book book = Book.me.findFirst("select * from book where bookid=?" , temp.get("bookid"));
						if (book.get("state").equals("D")){
							temp.set("id", book.get("id"));
							temp.set("creater", book.get("creater"));
							temp.set("ctime", Timestamp.valueOf(df.format(new Date())));
							temp.set("typeid", book.get("typeid"));
							temp.set("color", book.get("color"));
							temp.set("state", "D");
							book.setAttrs(temp.getAll());
							book.update();	
							Booktemp.me.deleteById(tempid);
						}
					}
				}
			}
		}
		//booktempService.remove(random);
		render(BjuiRender.success("覆盖成功！" , false).tabid(tabid));
	}
	
}
